package com.yzh.meiqia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yzh.meiqia.common.constant.Constants;
import com.yzh.meiqia.common.constant.KeyConstants;
import com.yzh.meiqia.entity.Content;
import com.yzh.meiqia.mapper.ContentMapper;
import com.yzh.meiqia.service.ContentService;
import com.yzh.meiqia.struct.PageResult;
import com.yzh.meiqia.vo.SearchVO;
import com.yzh.meiqia.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yzh
 * @since 2020-12-09
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private CacheManager cacheManager;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public PageResult<Content> page(Integer currPage, Integer pageSize, SearchVO vo) {
        // 分页
        Page<Content> contentPage = new Page<>(currPage, pageSize);

        // 条件查询
        QueryWrapper<Content> wrapper = new QueryWrapper<>();

        if (Objects.nonNull(vo)) {
            String keyWord = vo.getKeyWord();
            if (StringUtils.isNotEmpty(keyWord)) {
                wrapper.like("name", keyWord);
            }
        }
        contentPage = page(contentPage, wrapper);
        return PageResult.of(contentPage.getRecords(), contentPage.getTotal());
    }


    @Override
    public boolean saveContent(Content content) {
        return save(content);
    }

    @Override
    public boolean updateContent(Content content) {
        return updateById(content);
    }

    @Override
    public boolean delById(Long id) {
        return removeById(id);
    }

    @Override
    public boolean delByIds(List<Long> ids) {
        return removeByIds(ids);
    }

    @Override
    public List<Content> getList() {
        Cache cache = cacheManager.getCache("conentList");
        List<Content> result = null;
        Element element = cache.get(KeyConstants.KEY_CONTENTS);
        if(element!=null){
            result = (List<Content>) element.getObjectValue();
            if(!CollectionUtils.isEmpty(result)){
                return result;
            }
        }


        BoundListOperations ops = redisTemplate.boundListOps(KeyConstants.KEY_CONTENTS);
        List range = ops.range(0, -1);
        if(!CollectionUtils.isEmpty(result)){
            cache.put(new Element(KeyConstants.KEY_CONTENTS,range));
            return range;
        }

        LambdaQueryWrapper<Content> lambda = new QueryWrapper<Content>().lambda();
        lambda.eq(Content::getStatus,1).orderByDesc(Content::getSort);
        result = list(lambda);
        if(!CollectionUtils.isEmpty(result)){
            cache.put(new Element(KeyConstants.KEY_CONTENTS,result));
            ops.leftPushAll(result.toArray(new Content[result.size()]));
        }
        return result;
    }
}
